package cn.cloudplug.friendsrecommend.util;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ViewHolder {
	private SparseArray<View> views;
	private int position;
	private View convertView;
	public ViewHolder(Context context,ViewGroup parent,int layoutId,int position){
		this.position=position;
		views=new SparseArray<View>();
		convertView=LayoutInflater.from(context).inflate(layoutId, parent,false);
		convertView.setTag(this);
	}
	public static ViewHolder get(Context context,ViewGroup parent,int layoutId,int position,View convertView){
		if(convertView==null){
			return new ViewHolder(context, parent, layoutId, position);
		}else{
			ViewHolder holder=(ViewHolder) convertView.getTag();
			holder.position=position;
			return holder;
		}
	}
	public View getConvertView() {
		return convertView;
	}
	public <T extends View> T getView(int viewId){
		View view=views.get(viewId);
		if(view==null){
			view=convertView.findViewById(viewId);
			views.put(viewId, view);
		}
		return (T)view;
	}
	public ViewHolder setText(int viewId,String text){
		TextView tv=getView(viewId);
		tv.setText(text);
		return this;
	}
}
